package com.qdxwx.manage.controller.my;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.channel.ChannelService;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.data.service.teacher.TeacherService;
import com.qdxwx.manage.controller.BaseController;

import com.qdxwx.manage.support.SessionUtils;
import com.qdxwx.models.channel.Channel;
import com.qdxwx.models.course.Course;
import com.qdxwx.models.course.CourseQuery;
import com.qdxwx.models.enums.ModuleType;
import com.qdxwx.models.manager.Manager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 课程Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/my/course")
public class MyCourseController extends BaseController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private ChannelService channelService;

    @GetMapping("/list.html")
    public String list(HttpServletRequest request, ModelMap mmap) {
        Manager manager = SessionUtils.getManager();
        if (manager == null){
            return error404;
        }
        int channelId = TKConvert.toInteger(request.getParameter("channelId"));

        List<Channel> channelList = channelService.getMenusByModuleId(ModuleType.COURSE.ordinal());

        if (channelId==0 && channelList.size() > 0){
            channelId = channelList.get(0).getChannelId();
        }

        mmap.put("channelId", channelId);
        return "my/my-course-list";
    }

    /**
     * 查询课程列表
     */
    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        Manager manager = SessionUtils.getManager();
        if (manager == null) return TableDataInfo.error("未登录！");
        String courseName = request.getParameter("courseName");
        int channelId = TKConvert.toInteger(request.getParameter("channelId"));
        int isAvailable = TKConvert.toInteger(request.getParameter("isAvailable"));
        CourseQuery course = new CourseQuery();
        if (channelId > 0) {
            course.setChannelId(channelId);
        }
        course.setUserId(manager.getUserId());
        course.setCourseName(TKString.isNotEmpty(courseName) ? courseName : null);
        course.setIsAvailable(isAvailable > 0 ? isAvailable : null);
        List<Course> list = courseService.getList(course, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(courseService.count(course));
        return rspData;
    }
}
